home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / chap02 / howto09 / drwsutl2.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-03-05  |  77.3 KB  |  2,055 lines

  1. unit Drwsutl2;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, ShellAPI, FileCtrl, DRWSUtl1;
  8.  
  9. const
  10.   EOC_CHANGEDIR = 1;  { Error Operation Code for change directory failure }
  11.   EOC_SOURCECOPY = 2; { Error Operation Code for source copy failure      }
  12.   EOC_DESTCOPY = 3;   { Error Operation Code for destination copy failure }
  13.   EOC_DELETEFILE = 4; { Error Operation Code for file delete failure      }
  14.   EOC_DELETEDIR = 5;  { Error Operation Code for directory delete failure }
  15.   EOC_RENAMEFILE = 6; { Error Operation Code for renaming failure         }
  16.   EOC_MAKEDIR = 7;    { Error Operation Code for MkDir failure            }
  17.   EOC_SETATTR = 8;    { Error Operation Code for Set Attributes failure   }
  18.  
  19.   FAC_COPY = 1;       { File Action Code for recursive copying            }
  20.   FAC_MOVE = 2;       { File Action Code for recursive moving             }
  21.   FAC_DELETE = 3;     { File Action Code for recursive deletion           }
  22. type
  23.   { This is a descendant of TFileListbox }
  24.   { Which puts icons of files into the   }
  25.   { Objects array rather than the stand- }
  26.   { ard bitmaps.                         }
  27.   TIconFileListBox = class( TFileListBox )
  28.   public
  29.     { public methods and data }
  30.     procedure ReadFileNames; override;
  31.     function GetNextSelection( SourceDirectory : String;
  32.               var CurrentItem : Integer ) : String;
  33.     constructor Create(AOwner : TComponent); override; { override create    }
  34.     procedure TheDblClick( Sender : TObject );{ This holds override dblclick }
  35.   end;
  36.   TFileWorkBench = class( TComponent )
  37.   public
  38.     GlobalError        : Integer;  { This is used by FMXUCopyFile for er code }
  39.     GlobalErrorType    : Integer;  { This holds the Operation code            }
  40.     function ForceTrailingBackSlash( const TheFileName : String ) : String;
  41.     function StripNonRootTrailingBackSlash(
  42.               const TheFileName : String ) : String;
  43.     procedure GetFileAttributes( TheFile : String; var IsDirectory , IsArchive ,
  44.                 IsVolumeID , IsHidden , IsReadOnly , IsSysFile : Boolean );
  45.     procedure HandleIOException( TheOpCode : Integer; ThePath : String;
  46.                                  TheMessage : String; TheCode : Integer );
  47.     procedure HandleDOSError( TheOpCode : Integer; ThePath : String;
  48.                 TheCode : Integer );
  49.     function CopyFile( TargetPath ,
  50.                DestinationPath : String ) : Boolean;
  51.     procedure ChangeTheDirectory( NewPath : String );
  52.     procedure ChangeTheDriveAndDirectory( NewDrive : Integer );
  53.     procedure CopyTheFile( OldPath , NewPath : String );
  54.     procedure MoveTheFile( OldPath , NewPath : String );
  55.     procedure DeleteTheFile( ThePath : String );
  56.     procedure RenameTheFile( OldPath , NewName : String );
  57.     procedure CreateNewDirectory( NewPath : String );
  58.     procedure RemoveDirectory( ThePath : String );
  59.     procedure SetFileAttributes( TheFile  : String; TheAttributes : Integer );
  60.     procedure RecursivelyCopyDirectory( OldPath , NewPath : String );
  61.     procedure RecursivelyMoveDirectory( OldPath , NewPath : String );
  62.     procedure RecursivelyDeleteDirectory( ThePath : String );
  63.     procedure HandleRecursiveAction( StartingPath , NewPath : String;
  64.                ActionCode : Integer );
  65.   end;
  66.   TFileIconPanel = class( TPanel )
  67.   private
  68.     { Private declarations }
  69.     FHighlightColor : TColor;                 { This holds bright edge bevel }
  70.     FShadowColor    : TColor;                 { This holds dark edge bevel   }
  71.     procedure TheClick( Sender : TObject );   { This holds override click    }
  72.     procedure TheDblClick( Sender : TObject );{ This holds override dblclick }
  73.     procedure TheMouseDown(Sender: TObject;
  74.       Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  75.     procedure TheMouseUp(Sender: TObject;
  76.       Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  77.   protected                                   { event method procedure.      }
  78.     { Protected declarations }
  79.     procedure Paint; override;                { This allows custom painting  }
  80.   public
  81.     { Public declarations }
  82.     FTheIcon : TIcon;                         { This is the display icon    }
  83.     FTheName : String;                        { This is the filename        }
  84.     FTheLabel : TLabel;                       { This is the display label   }
  85.     Selected : Boolean;                       { This holds selection status }
  86.     constructor Create(AOwner : TComponent); override; { override create    }
  87.     procedure Initialize( PanelX              ,             { Left          }
  88.                           PanelY              ,             { Top           }
  89.                           PanelWidth          ,             { Width         }
  90.                           PanelHeight         ,             { Height        }
  91.                           PanelBevelWidth     ,             { Bevel Width   }
  92.                           LabelFontSize         : Integer;  { Font size     }
  93.                           PanelColor          ,             { Main color    }
  94.                           PanelHighlightColor ,             { Bright color  }
  95.                           PanelShadowColor    ,             { Dark color    }
  96.                           LabelTextColor        : TColor;   { Text color    }
  97.                           TheFilename         ,             { Filename      }
  98.                           LabelFontName         : String;   { Font name     }
  99.                           LabelFontStyle        : TFontStyles;  { Font style}
  100.                           ExtraData             : Integer       );  { Drive }
  101.     destructor Destroy; override;             { override destroy to free    }
  102.   end;
  103.   TFileIconPanelScrollBox = class( TScrollBox )
  104.   public
  105.     { Public methods and data }
  106.     TheFWB              : TFileWorkBench; { Used for file manipulation         }
  107.     IconsNeedRefreshing : Boolean;                   { Flag to redo display    }
  108.     TheIconSize        : Integer;   { Holds Individual Icon size               }
  109.     TheIconSpacing     : Integer;   { Holds total icon footprint               }
  110.     MaxIconsInARow     : Integer;   { Set for screen size.                     }
  111.     TheStoredHandle    : HWnd;
  112.     procedure Update;                                { Called to reset display }
  113.     constructor Create( AOwner : TComponent ); override;  { Override inherited }
  114.     procedure ClearTheFIPs;                          { Clears the FIPs safely  }
  115.     procedure AddDriveIcons( var XCounter , YCounter : Integer ); { Add drives }
  116.     procedure GetColorsForFileIcon( TheFile : String;
  117.                var BC , HC , SC , TC : TColor );
  118.     procedure GetIconsForEntireDirectory( TargetPath  : String );
  119.     function GetNextSelection( SourceDirectory : String;
  120.               var CurrentItem : Integer ) : String;
  121.     procedure DisplayRecursiveSearchResults(
  122.       TheStartingDirectory : String );
  123.   end;
  124.  
  125.   { This procedure gets an icon for a file using FindExecutable  }
  126.   { and ExtractIcon. (assumes file/dir is passed)                }
  127.   procedure GetIconForFile( TheName : String; var TheIcon : TIcon );
  128.   { This procedure spaces out the bitbtn components on a tpanel }
  129.   procedure SpacePanelButtons( WhichPanel : TPanel );
  130.     procedure FMXUCopyFile(const FileName, DestName: String; var GlobalErrorType ,
  131.                GlobalErrorCode : Integer );
  132.  
  133. implementation
  134. {$R DRWSUTL2.RES}                 { Import custom resource file }
  135.  
  136. { It has been edited to return viable error codes!             }
  137. procedure FMXUCopyFile(const FileName, DestName: String; var GlobalErrorType ,
  138.             GlobalErrorCode : Integer );
  139. var
  140.   CopyBuffer: Pointer; { buffer for copying }
  141.   BytesCopied: Longint;
  142.   TheAttr : Integer;
  143.   Source, Dest: Integer; { handles }
  144. const
  145.   ChunkSize: Longint = 8192; { copy in 8K chunks }
  146. begin
  147.   GetMem(CopyBuffer, ChunkSize); { allocate the buffer }
  148.   Source := FileOpen(FileName, fmShareDenyWrite); { open source file }
  149.   if Source < 0 then
  150.   begin  { error creating source file }
  151.     GlobalErrorType := EOC_SOURCECOPY;
  152.     GlobalErrorCode := -IOResult;
  153.     if GlobalErrorCode = 0 then GlobalErrorCode := -157;
  154.     FreeMem( CopyBuffer, ChunkSize );
  155.     exit;
  156.   end;
  157.   Dest := FileCreate(DestName); { create output file; overwrite existing }
  158.   if Dest < 0 then
  159.   begin  { error creating destination file }
  160.     FileClose( Source );
  161.     GlobalErrorType := EOC_DESTCOPY;
  162.     GlobalErrorCode := -IOResult;
  163.     if GlobalErrorCode = 0 then GlobalErrorCode := -159;
  164.     FreeMem( CopyBuffer , ChunkSize );
  165.     exit;
  166.   end;
  167.   {$I-}
  168.   repeat
  169.     BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize); { read chunk}
  170.     if BytesCopied > 0 then { if we read anything... }
  171.     FileWrite(Dest, CopyBuffer^, BytesCopied); { ...write chunk }
  172.   until BytesCopied < ChunkSize; { until we run out of chunks }
  173.   {$I+}
  174.   GlobalErrorCode := -IOResult;  { get any error code which happens during copying }
  175.   FileClose(Dest); { close the destination file }
  176.   FileClose(Source); { close the source file }
  177.   FreeMem(CopyBuffer, ChunkSize); { free the buffer }
  178. end;
  179.  
  180. { This procedure spaces out the bitbtn components on a tpanel }
  181. procedure SpacePanelButtons( WhichPanel : TPanel );
  182. var TheCalculatedSpacing     ,            { Holds primary spacing }
  183.     TheFullCalculatedSpacing   : Integer; { Holds full spacing    }
  184.     Counter_1                  : Integer; { Loop counter          }
  185.     TotalIBs                   : Integer; { Gets total buttons    }
  186. begin
  187.   { Set up spacing values }
  188.   TotalIBs := WhichPanel.ControlCount;
  189.   TheCalculatedSpacing := (( WhichPanel.Width - 6 - ( TotalIbs * 49 ))
  190.    div ( TotalIbs + 1 ));
  191.   TheFullCalculatedSpacing := TheCalculatedSpacing + 49;
  192.   { Loop through all imported buttons and set their Left values }
  193.   for Counter_1 := 1 to WhichPanel.ControlCount do
  194.   begin
  195.     if Counter_1 = 1 then
  196.     begin
  197.       TBitBtn( WhichPanel.Controls[ Counter_1 - 1 ] ).Left := 3 +
  198.        TheCalculatedSpacing;
  199.     end
  200.     else
  201.     begin
  202.       TBitBtn( WhichPanel.Controls[ Counter_1 - 1 ] ).Left := 3 +
  203.        (( Counter_1 - 1 ) * TheFullCalculatedSpacing ) + TheCalculatedSpacing;
  204.     end;
  205.   end;
  206. end;
  207.  
  208. { This procedure gets an icon for a file using FindExecutable  }
  209. { and ExtractIcon. (assumes file/dir is passed)                }
  210. procedure GetIconForFile( TheName : String; var TheIcon : TIcon );
  211. var TheExt           : String; { File extension holder }
  212.     TheOtherPChar  ,           { Windows ASCIIZ string }
  213.     ThePChar         : PChar;  { Windows ASCIIZ string }
  214.     Dummy : Word;
  215. begin
  216.   { Check for directory and if so get directory icon from RES file }
  217.   if (( FileGetAttr( TheName ) and faDirectory ) = faDirectory ) then
  218.   begin
  219.     { Set up the PChar to communicate with Windows }
  220.     GetMem( TheOtherPChar , 255 );
  221.     { Convert Pascal-style string to ASCIIZ Pchar }
  222.     StrPCopy( TheOtherPChar , 'DIRECTORY' );
  223.     { Use API call to return icon handle of Icon Resource in FILECTRL.RES }
  224.     TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  225.     { Release memory from PChar }
  226.     FreeMem( TheOtherPChar , 255 );
  227.     { Leave }
  228.     exit;
  229.   end;
  230.   { Assume archive file; get its extension }
  231.   TheExt := Uppercase( ExtractFileExt( TheName ));
  232.   { If not an executable/image file then use FindExecutable to get icon }
  233.   if (( TheExt <> '.EXE' ) and ( TheExt <> '.BAT' ) and
  234.       ( TheExt <> '.PIF' ) and ( TheExt <> '.COM' )) then
  235.   begin
  236.     { Grab three chunks of memory }
  237.     GetMem( ThePChar , 255 );
  238.     { Set up the name and its directory in Windows string formats }
  239.     StrPCopy( ThePChar, TheName );
  240.     Dummy := 65535;
  241.     {**** Windows 95 Specialized call ****** }
  242.     TheIcon.Handle := ExtractAssociatedIcon( hInstance , ThePChar , Dummy );
  243.     if TheIcon.Handle = 0 then
  244.     begin
  245.       GetMem( TheOtherPChar , 255 );
  246.       StrPCopy( TheOtherPChar , 'NOICON' );
  247.       TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  248.       FreeMem( TheOtherPChar , 255 );
  249.       exit;
  250.     end;
  251.     FreeMem( ThePChar , 255 );
  252.   end
  253.   else
  254.   { Assume Windows Executable file, so get icon from it with ExtractIcon API }
  255.   begin
  256.     GetMem( ThePChar , 255 );
  257.     StrPCopy( ThePChar , TheName );
  258.     { Try to get first icon for file }
  259.     Dummy := 65535;
  260.     TheIcon.Handle := ExtractAssociatedIcon( hInstance , ThePChar , Dummy );
  261.     FreeMem( ThePChar , 255 );
  262.     { If handle is 0 invalid icon format so use default from RES file }
  263.     if TheIcon.Handle = 0 then
  264.     begin
  265.       GetMem( TheOtherPChar , 255 );
  266.       StrPCopy( TheOtherPChar , 'NOICON' );
  267.       TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  268.       FreeMem( TheOtherPChar , 255 );
  269.       exit;
  270.     end;
  271.   end;
  272. end;
  273.  
  274. { This procedure does a fully error-trapped change directory }
  275. procedure TFileWorkBench.ChangeTheDirectory( NewPath : String );
  276. var CurrentDirectory : String;
  277. begin
  278.   if NewPath = '..' then
  279.   begin { Back up one level }
  280.     {$I+}
  281.     try
  282.       { Find the current directory }
  283.       GetDir( 0 , CurrentDirectory );
  284.       { Use EFP to move up one level }
  285.       CurrentDirectory := ExtractFilePath( CurrentDirectory );
  286.       { Strip trailing \ if not root }
  287.       CurrentDirectory := StripNonRootTrailingBackSlash( CurrentDirectory );
  288.       { Try the change to the new drive }
  289.       ChDir( CurrentDirectory );
  290.     except
  291.       { if any exception occurs instantiate exception and show }
  292.       On E:EInOutError do
  293.       begin
  294.         { Call custom error display/lookup procedure }
  295.         HandleIOException( EOC_CHANGEDIR , CurrentDirectory ,
  296.          E.Message , E.ErrorCode );
  297.       end;
  298.     end;
  299.   end
  300.   else
  301.   begin { Change to explicit path }
  302.     {$I+}
  303.     try
  304.       { Get target directory path }
  305.       CurrentDirectory := NewPath;
  306.       { Strip trailing \ if not root }
  307.       CurrentDirectory := StripNonRootTrailingBackSlash( CurrentDirectory );
  308.       { Try the change to the new drive }
  309.       ChDir( CurrentDirectory );
  310.     except
  311.       { if any exception occurs instantiate exception and show }
  312.       On E:EInOutError do
  313.       begin
  314.         { Call custom error display/lookup procedure }
  315.         HandleIOException( EOC_CHANGEDIR , CurrentDirectory ,
  316.          E.Message , E.ErrorCode );
  317.       end;
  318.     end;
  319.   end;
  320. end;
  321.  
  322. { This procedure does a fully error-trapped change directory }
  323. procedure TFileWorkBench.ChangeTheDriveAndDirectory( NewDrive : Integer );
  324. var CurrentDirectory : String;
  325. begin
  326.   {$I+}
  327.   try
  328.     { Find the working directory on new drive }
  329.     GetDir( NewDrive , CurrentDirectory );
  330.     { Try the change to the new drive }
  331.     ChDir( CurrentDirectory );
  332.   except
  333.     { if any exception occurs instantiate exception and show }
  334.     On E:EInOutError do
  335.     begin
  336.       { Call custom error display/lookup procedure }
  337.       HandleIOException( EOC_CHANGEDIR , CurrentDirectory ,
  338.        E.Message , E.ErrorCode );
  339.     end;
  340.   end;
  341. end;
  342.  
  343. { This procedure copies a single file with error trapping }
  344. procedure TFileWorkBench.CopyTheFile( OldPath , NewPath : String );
  345. var AResult : Boolean; { Internal data flag }
  346. begin
  347.   { If Copyfile returns false an error occurred }
  348.   AResult := CopyFile( OldPath , NewPath +
  349.    ExtractFileName( OldPath ));
  350.   { Display meaningful error message }
  351.   if not AResult then HandleDOSError( GlobalErrorType , OldPath, GlobalError );
  352. end;
  353.  
  354. { This procedure moves a file by copying and delete it }
  355. procedure TFileWorkBench.MoveTheFile( OldPath , NewPath : String );
  356. var AResult : Boolean; { Internal data flag }
  357.     TheFile : File;    { Use to get errors  }
  358. begin
  359.   { If Copyfile returns false an error occurred }
  360.   AResult := CopyFile( OldPath , NewPath +
  361.     ExtractFileName( OldPath ));
  362.   { Display meaningful error message }
  363.   if not AResult then HandleDOSError( GlobalErrorType ,
  364.     OldPath , GlobalError );
  365.   { After valid copying, delete source file }
  366.   {$I+}
  367.   if AResult then try
  368.     { Use this trick to get valid exception handling }
  369.     AssignFile( TheFile , OldPath );
  370.     { Use erase because Deletefile doesn't give exceptions! }
  371.     Erase( TheFile );
  372.   except
  373.     { if any exception occurs instantiate exception and show }
  374.     On E:EInOutError do
  375.     begin
  376.       { Call custom error display/lookup procedure }
  377.       HandleIOException( EOC_DELETEFILE , OldPath ,
  378.        E.Message , E.ErrorCode );
  379.     end;
  380.   end;
  381. end;
  382.  
  383. { This procedure safely deletes a single file }
  384. procedure TFileWorkBench.DeleteTheFile( ThePath : String );
  385. var TheFile : File; { Internal file handle }
  386. begin
  387.   {$I+}
  388.   try
  389.     { Use this trick to get valid exception handling }
  390.     AssignFile( TheFile , ThePath );
  391.     { Use erase because Deletefile doesn't give exceptions! }
  392.     Erase( TheFile );
  393.   except
  394.     { if any exception occurs instantiate exception and show }
  395.     On E:EInOutError do
  396.     begin
  397.       { Call custom error display/lookup procedure }
  398.       HandleIOException( EOC_DELETEFILE , ThePath ,
  399.        E.Message , E.ErrorCode );
  400.     end;
  401.   end;
  402. end;
  403.  
  404. { This procedure renames a file with full error trapping }
  405. procedure TFileWorkBench.RenameTheFile( OldPath , NewName : String );
  406. var TheFile : File; { Internal file handle }
  407. begin
  408.   {$I+}
  409.   try
  410.     { Use this trick to get valid exception handling }
  411.     AssignFile( TheFile , OldPath );
  412.     { Use this because RenameFile doesn't give exceptions! }
  413.     Rename( TheFile , NewName );
  414.   except
  415.     { if any exception occurs instantiate exception and show }
  416.     On E:EInOutError do
  417.     begin
  418.       { Call custom error display/lookup procedure }
  419.       HandleIOException( EOC_RENAMEFILE , OldPath  ,
  420.        E.Message , E.ErrorCode );
  421.     end;
  422.   end;
  423. end;
  424.  
  425. { This procedure creates a new directory with full error trapping }
  426. procedure TFileWorkBench.CreateNewDirectory( NewPath : String );
  427. begin
  428.   {$I+}
  429.   try
  430.     Mkdir( NewPath );
  431.   except
  432.     { if any exception occurs instantiate exception and show }
  433.     On E:EInOutError do
  434.     begin
  435.       { Call custom error display/lookup procedure }
  436.       HandleIOException( EOC_MAKEDIR , NewPath  ,
  437.        E.Message , E.ErrorCode );
  438.     end;
  439.   end;
  440. end;
  441.  
  442. { This procedure remove a directory with full error trapping }
  443. procedure TFileWorkBench.RemoveDirectory( ThePath : String );
  444. begin
  445.   {$I+}
  446.   try
  447.     Rmdir( ThePath );
  448.   except
  449.     { if any exception occurs instantiate exception and show }
  450.     On E:EInOutError do
  451.     begin
  452.       { Call custom error display/lookup procedure }
  453.       HandleIOException( EOC_DELETEDIR , ThePath  ,
  454.        E.Message , E.ErrorCode );
  455.     end;
  456.   end;
  457. end;
  458.  
  459. { Use this to set the attributes of a file with error trapping }
  460. procedure TFileWorkBench.SetFileAttributes( TheFile  : String;
  461.            TheAttributes : Integer );
  462. var TheResult : Integer; { Holds error code if any }
  463. begin
  464.   { Attempt to set the attributes }
  465.   TheResult := FileSetAttr( TheFile , TheAttributes );
  466.   { if negative number error, so signal }
  467.   if TheResult < 0 then
  468.    HandleDOSError( EOC_SETATTR , TheFile , -TheResult );
  469. end;
  470.  
  471. { This procedure recursively copies a directory to a new path }
  472. procedure TFileWorkBench.RecursivelyCopyDirectory( OldPath , NewPath : String );
  473. var TheDir : String; { Holds source directory }
  474. begin
  475.   { Get the source directory to copy }
  476.   TheDir := ExtractFileName( OldPath );
  477.   { Force a backslash to the newpath variable }
  478.   NewPath := ForceTrailingBackSlash( NewPath );
  479.   { Add the source directory to the target path }
  480.   NewPath := NewPath + TheDir;
  481.   { Create a new directory with the new name }
  482.   CreateNewDirectory( NewPath );
  483.   { Force a backslash for compatibility }
  484.   NewPath := FOrcetrailingBackSlash( NewPath );
  485.   { Do the recursive call }
  486.   HandleRecursiveAction( OldPath , NewPath , FAC_COPY );
  487. end;
  488.  
  489. { This procedure recursively moves a directory tree }
  490. procedure TFileWorkBench.RecursivelyMoveDirectory( OldPath , NewPath : String );
  491. var TheDir    : String; { Holds source directory  }
  492.     SavedPath : String; { Holds saved dir to kill }
  493. begin
  494.   { Get the source directory to move }
  495.   TheDir := ExtractFileName( OldPath );
  496.   { Force a backslash to the newpath variable }
  497.   NewPath := ForceTrailingBackSlash( NewPath );
  498.   { Save the starting path just in case }
  499.   SavedPath := OldPath;
  500.   { Add the source directory to the target path }
  501.   NewPath := NewPath + TheDir;
  502.   { Create a new directory with the new name }
  503.   CreateNewDirectory( NewPath );
  504.   { Force a backslash for compatibility }
  505.   NewPath := FOrcetrailingBackSlash( NewPath );
  506.   { Do the recursive call }
  507.   HandleRecursiveAction( OldPath , NewPath , FAC_MOVE );
  508.   { Remove the source directory }
  509.   RemoveDirectory( SavedPath );
  510. end;
  511.  
  512. { This procedure handles recursively deleting an entire directory tree }
  513. procedure TFileWorkBench.RecursivelyDeleteDirectory( ThePath : String );
  514. begin
  515.   HandleRecursiveAction( ThePath , '' , FAC_DELETE );
  516. end;
  517.  
  518.  
  519. { This is the generic routine to copy, move, and delete whole directory trees }
  520. procedure TFileWorkBench.HandleRecursiveAction( StartingPath , NewPath : String;
  521.            ActionCode : Integer );
  522. { VITAL!!! These variables MUST be local for recursrion to work! }
  523. var
  524.     Finished        : Boolean;         { Loop flag              }
  525.     TheSR           : TSearchRec;      { Searchrecord for FF/FN }
  526.     TheResult       : Integer;         { return variable        }
  527.     TargetPath ,
  528.     FileMask   ,
  529.     TheWorkingDirectory ,
  530.     TheStoredWorkingDirectory ,
  531.     ModifiedDirectory  : String;       { path for FF/FN         }
  532.     TheFIP          : TFileIconPanel;  { generic FIP holder     }
  533.     ButtonColor   ,                    { main panel color       }
  534.     ButtonHLColor ,                    { bright panel color     }
  535.     ButtonSColor  ,                    { dark panel color       }
  536.     Textcolor       : TColor;          { label text color       }
  537.     TheFile         : File;
  538.  
  539. begin
  540.   { Set up the initial variables }
  541.   Finished := false;
  542.   TheWorkingDirectory := StartingPath;
  543.   TheStoredWorkingDirectory := TheWorkingDirectory;
  544.   TheWorkingDirectory := TheWorkingDirectory + '\*.*';
  545.   TargetPath := ExtractFilePath( TheWorkingDirectory );
  546.   { Make the call to FindFirst set to get any file }
  547.   TheResult := FindFirst( TheWorkingDirectory , faAnyFile , TheSR );
  548.   { loop through all files in the directory and delete them }
  549.   while not Finished do
  550.   begin
  551.     { Make call to FindNext, using only SearchRecord from FindFirst }
  552.     TheResult := FindNext( TheSR );
  553.     { A -1 result means no more files so exit }
  554.     if TheResult <> 0 then finished := true else
  555.     begin
  556.       if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory )
  557.        <> faDirectory ) then
  558.       begin { A File }
  559.         case ActionCode of
  560.           FAC_COPY :
  561.               begin
  562.                 CopyTheFile( TargetPath + TheSR.Name , NewPath );
  563.               end;
  564.           FAC_MOVE :
  565.               begin
  566.                 MoveTheFile( TargetPath + TheSR.Name , NewPath );
  567.               end;
  568.           FAC_DELETE :
  569.               begin { Delete }
  570.                 if MessageDlg( 'Delete file ' + TargetPath + TheSR.Name + '?',
  571.                    mtConfirmation , mbYesNoCancel , 0 ) = mrYes then
  572.                     DeleteTheFile( TargetPath + TheSR.Name );
  573.               end;
  574.         end;
  575.       end;
  576.     end;
  577.   end;
  578.   { Set up the variables to do recursive calls on all directories}
  579.   Finished := false;
  580.   ModifiedDirectory := TheStoredWorkingdirectory + '\*.*';
  581.   { Make the call to FindFirst set to get any file, ignore result }
  582.   TheResult := FindFirst( ModifiedDirectory , faDirectory , TheSR );
  583.   while not Finished do
  584.   begin
  585.     { Make call to FindNext, using only SearchRecord from FindFirst }
  586.     TheResult := FindNext( TheSR );
  587.     { A -1 result means no more files so exit }
  588.     if TheResult <> 0 then
  589.       finished := true
  590.     else
  591.     begin
  592.       if TheSR.Name <> '..' then { Ignore backup in this case }
  593.       begin
  594.         if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory )
  595.          = faDirectory ) then
  596.         begin
  597.           { Send in the new directory name }
  598.           ModifiedDirectory := TheStoredWorkingDirectory  + '\' +
  599.            TheSR.Name;
  600.           { Reproduce directory structure for recursion in copy/move }
  601.           NewPath := NewPath + TheSR.Name;
  602.           case ActionCode of
  603.             FAC_COPY , FAC_MOVE :
  604.                begin { Create ahead for move and copy }
  605.                  { Make the new directory for moving and copying }
  606.                  CreateNewDirectory( NewPath );
  607.                  { Force a backslash for compatibility }
  608.                  NewPath := ForceTrailingBackSlash( NewPath );
  609.                end;
  610.             FAC_DELETE :
  611.                begin  { No prior action needed for Delete }
  612.                end;
  613.           end;
  614.           { Do the recursive call }
  615.           HandleRecursiveAction( ModifiedDirectory , NewPath , ActionCode );
  616.           case ActionCode of
  617.             FAC_COPY :
  618.                begin { no action for copy }
  619.                end;
  620.             FAC_MOVE , FAC_DELETE :
  621.                begin  { Delete }
  622.                  { Get a confirmation }
  623.                  if MessageDlg( 'Remove Directory ' + TargetPath + TheSR.Name
  624.                   + '?', mtConfirmation , mbYesNoCancel , 0 ) = mrYes then
  625.                    RemoveDirectory( TargetPath + TheSR.Name );
  626.                end;
  627.           end;
  628.         end;
  629.       end;
  630.     end;
  631.   end;
  632. end;
  633.  
  634. { This is a generic copy routine taken from Delphi sample code }
  635. { This function calls the sample Copy code and handles errors }
  636. function TFileWorkBench.CopyFile( TargetPath ,
  637.           DestinationPath : String ) : Boolean;
  638. begin
  639.   { Set global error value to no error }
  640.   GlobalError := 0;
  641.   { Call the sample procedure to do the copy }
  642.   FMXUCopyFile( TargetPath, DestinationPath , GlobalErrorType , GlobalError );
  643.   { If no error return true else return false }
  644.   if GlobalError < 0 then CopyFile := false else
  645.    CopyFile := true;
  646. end;
  647.  
  648. { This procedure handles displaying a user-friendly Dialog box with a }
  649. { Message for Delphi IO exception errors.                             }
  650. procedure TFileWorkBench.HandleIOException( TheOpCode : Integer;
  651.            ThePath : String; TheMessage : String; TheCode : Integer );
  652. var ErrorMessageString : String;  { Holds internal data }
  653.     OperationString    : String;  { Holds internal data }
  654. begin
  655.   { clear to check for unrecognized code }
  656.   ErrorMessageString := '';
  657.   { Check against imported code }
  658.   case TheCode of
  659.     2    : ErrorMessageString := 'File not found';
  660.     3    : ErrorMessageString := 'Path not found';
  661.     4    : ErrorMessageString := 'Too many open files';
  662.     5    : ErrorMessageString := 'File access denied';
  663.     6    : ErrorMessageString := 'Invalid file handle';
  664.     12    : ErrorMessageString := 'Invalid file access code';
  665.     15    : ErrorMessageString := 'Invalid drive number';
  666.     16  : ErrorMessageString := 'Cannot remove current directory';
  667.     17    : ErrorMessageString := 'Cannot rename across drives';
  668.     100    : ErrorMessageString := 'Disk read error';
  669.     101    : ErrorMessageString := 'Disk write error';
  670.     102    : ErrorMessageString := 'File not assigned';
  671.     103    : ErrorMessageString := 'File not open';
  672.     104    : ErrorMessageString := 'File not open for input';
  673.     105    : ErrorMessageString := 'File not open for output';
  674.   end;
  675.   case TheOpCode of
  676.     EOC_CHANGEDIR : OperationString := 'Unable to Change Directory due to ';
  677.     EOC_SOURCECOPY : OperationString := 'Unable to Copy due to Source File ';
  678.     EOC_DESTCOPY : OperationString := 'Unable to Copy due to Destination File ';
  679.     EOC_DELETEFILE : OperationString := 'Unable to Delete File due to ';
  680.     EOC_DELETEDIR : OperationString := 'Unable to Delete Directory due to ';
  681.     EOC_RENAMEFILE : OperationString := 'Unable to Rename File due to ';
  682.     EOC_MAKEDIR : OperationString := 'Unable to Create New Directory due to ';
  683.     EOC_SETATTR : OperationString := 'Unable to Set File Attributes due to ';
  684.   end;
  685.   { If not recognized use message; not a DOS error; reset cursor for neatness }
  686.   if ErrorMessageString = '' then
  687.   begin
  688.     Screen.Cursor := crDefault;
  689.     MessageDlg( OperationString + ExtractFileName( ThePath ) + ' ' +
  690.      TheMessage , mtError , [mbOK],0);
  691.   end
  692.   else
  693.   begin
  694.     { Recognized DOS exception, reset cursor for neatness }
  695.     Screen.Cursor := crDefault;
  696.     MessageDlg( OperationString + ExtractFileName( ThePath ) + ' ' +
  697.      ErrorMessageString , mtError , [mbOK], 0 );
  698.   end;
  699. end;
  700.  
  701. { This procedure handles displaying a user-friendly Dialog box with a }
  702. { Message for DOS error codes.                                        }
  703. procedure TFileWorkBench.HandleDOSError( TheOpCode : Integer;
  704.            ThePath : String;  TheCode : Integer );
  705. var ErrorMessageString : String;  { internal message holder }
  706.     OperationString : String;     { internal message holder }
  707. begin
  708.   { clear the message holder to check for unrecognized code }
  709.   ErrorMessageString := '';
  710.   { Negate the code back to normal number and check to set string }
  711.   case -TheCode of
  712.     2    : ErrorMessageString := 'File not found';
  713.     3    : ErrorMessageString := 'Path not found';
  714.     4    : ErrorMessageString := 'Too many open files';
  715.     5    : ErrorMessageString := 'File access denied';
  716.     6    : ErrorMessageString := 'Invalid file handle';
  717.     12    : ErrorMessageString := 'Invalid file access code';
  718.     15    : ErrorMessageString := 'Invalid drive number';
  719.     16  : ErrorMessageString := 'Cannot remove current directory';
  720.     17    : ErrorMessageString := 'Cannot rename across drives';
  721.     100    : ErrorMessageString := 'Disk read error';
  722.     101    : ErrorMessageString := 'Disk write error';
  723.     102    : ErrorMessageString := 'File not assigned';
  724.     103    : ErrorMessageString := 'File not open';
  725.     104    : ErrorMessageString := 'File not open for input';
  726.     105    : ErrorMessageString := 'File not open for output';
  727.     157 : ErrormessageString := 'Could not open Source File';
  728.     159 : ErrormessageString := 'Could not open Target File';
  729.   end;
  730.   case TheOpCode of
  731.     EOC_CHANGEDIR : OperationString := 'Unable to Change Directory due to ';
  732.     EOC_SOURCECOPY : OperationString := 'Unable to Copy due to Source File ';
  733.     EOC_DESTCOPY : OperationString := 'Unable to Copy due to Destination File ';
  734.     EOC_DELETEFILE : OperationString := 'Unable to Delete File due to ';
  735.     EOC_DELETEDIR : OperationString := 'Unable to Delete Directory due to ';
  736.     EOC_RENAMEFILE : OperationString := 'Unable to Rename File due to ';
  737.     EOC_MAKEDIR : OperationString := 'Unable to Create New Directory due to ';
  738.     EOC_SETATTR : OperationString := 'Unable to Set File Attributes due to ';
  739.   end;
  740.   { If the string is empty an unrecognized code was sent in }
  741.   if ErrorMessageString = '' then
  742.   begin
  743.     { Sent up db based on source or target error; reset cursor for neatness }
  744.     Screen.Cursor := crDefault;
  745.     MessageDlg( OperationString + ExtractFileName( ThePath ) + ' Error Code: ' +
  746.      IntToStr( TheCode ) , mtError , [mbOK],0);
  747.   end
  748.   else  { Code is recognized, use message from case statement }
  749.   begin
  750.     { Format the output for source or target error }
  751.     Screen.Cursor := crDefault;
  752.     MessageDlg( OperationString + ExtractFileName( ThePath ) + ' ' +
  753.      ErrorMessageString , mtError , [mbOK], 0 );
  754.   end;
  755. end;
  756.  
  757. { This procedure sets the imported booleans to the file's attributes }
  758. procedure TFileWorkBench.GetFileAttributes( TheFile : String; var IsDirectory ,
  759.            IsArchive , IsVolumeID , IsHidden , IsReadOnly ,
  760.             IsSysFile : Boolean );
  761. var TheResult : Integer; { Traps for error code on VolumeID }
  762. begin
  763.   { Clear the imported flags for default }
  764.   IsDirectory := false;
  765.   IsArchive := false;
  766.   IsVolumeID := false;
  767.   IsHidden := False;
  768.   IsReadOnly := false;
  769.   IsSysFile := false;
  770.   { Make the Dos call }
  771.   TheResult := FileGetAttr( TheFile );
  772.   if TheResult < 0 then
  773.   begin
  774.     { Volume ID returns -2 (?) }
  775.     IsVolumeID := true;
  776.     { It has no other properties }
  777.     exit;
  778.   end;
  779.   { Use AND test to set all other properties }
  780.   if (( TheResult and faDirectory ) = faDirectory ) then IsDirectory := true;
  781.   if (( TheResult and faArchive ) = faArchive ) then IsArchive := true;
  782.   if (( TheResult and faVolumeID ) = faVolumeID ) then IsVolumeID := true;
  783.   if (( TheResult and faReadOnly ) = faReadOnly ) then IsReadOnly := true;
  784.   if (( TheResult and faHidden ) = faHidden ) then IsHidden := true;
  785.   if (( TheResult and faSysFile ) = faSysFile ) then IsSysFile := true;
  786. end;
  787.  
  788. { This function makes sure a pathname has a trailing \ }
  789. function TFileWorkBench.ForceTrailingBackSlash(
  790.           const TheFileName : String ) : String;
  791. var TempString : String;  { Used to hold function result }
  792. begin
  793.   { If no trailing \ add one (root will already have one.) }
  794.   if TheFileName[ Length( TheFileName ) ] <> '\' then
  795.    TempString := TheFileName + '\' else TempString := TheFileName;
  796.   { Return modified or non-modified string }
  797.   ForceTrailingBackslash := TempString;
  798. end;
  799.  
  800. { This function makes sure a non-root dir has no trailing \ }
  801. function TFileWorkBench.StripNonRootTrailingBackSlash(
  802.           const TheFileName : String ) : String;
  803. var TempString : String ; { Used to hold function result }
  804. begin
  805.   { Default is no change }
  806.   TempString := TheFileName;
  807.   { If not root then }
  808.   if Length( TheFileName ) > 3 then
  809.   begin
  810.     { If has a trailing backslash remove it }
  811.     if TheFileName[ Length( TheFileName )] = '\' then
  812.     begin
  813.       TempString := Copy( TheFileName , 1 ,
  814.        Length( TheFileName ) - 1 );
  815.     end;
  816.   end;
  817.   { Export the final result }
  818.   StripNonRootTrailingBackSlash := TempString;
  819. end;
  820.  
  821. { This gets the next selected listbox item }
  822. function TIconFileListBox.GetNextSelection( SourceDirectory : String;
  823.           var CurrentItem : Integer ): String;
  824. var TheResult : String;  { Internal storage }
  825.     finished  : boolean; { Loop flag        }
  826. begin
  827.   { If out of items to check signal and exit }
  828.   if CurrentItem > Items.Count then TheResult := '' else
  829.   begin
  830.     { Otherwise scan from current position till match or end }
  831.     finished := false;
  832.     while not finished do
  833.     begin
  834.       { Check against selected property }
  835.       if Selected[ CurrentItem - 1 ] then
  836.       begin
  837.         { If selected then return it and abort loop }
  838.         TheResult := SourceDirectory + Items[ CurrentItem - 1 ];
  839.         finished := true;
  840.         { Increment current position }
  841.         CurrentItem := CurrentItem + 1;
  842.      end
  843.       else
  844.       begin
  845.         { Increment current position }
  846.         CurrentItem := CurrentItem + 1;
  847.         { Otherwise check for end of data and abort if out of entries }
  848.         if CurrentItem > Items.Count then
  849.         begin
  850.           TheResult := '';
  851.           finished := true;
  852.         end;
  853.       end;
  854.     end;
  855.   end;
  856.   { Return stored result }
  857.   GetNextSelection := TheResult;
  858. end;
  859.  
  860. { Modified from VCL Source Copyright 1995 }
  861. { Borland International, Inc.             }
  862. { Use this to override display with icons }
  863. procedure TIconFileListBox.ReadFileNames;
  864. var
  865.   AttrIndex   : TFileAttr;
  866.   i           : Integer;
  867.   FileExt     : string;
  868.   MaskPtr     : PChar;
  869.   Ptr         : PChar;
  870.   AttrWord    : Word;
  871.   TempPicture : TPicture;
  872.   TempBmp     : TBitmap;
  873.   TempIcon    : TIcon;
  874. const
  875.   Attributes: array[TFileAttr] of Word =
  876.   ( DDL_READONLY , DDL_HIDDEN , DDL_SYSTEM , $0008 , DDL_DIRECTORY ,
  877.     DDL_ARCHIVE  , DDL_EXCLUSIVE );
  878. begin
  879.   { if no handle allocated yet, this call will force         }
  880.   { one to be allocated incorrectly (i.e. at the wrong time. }
  881.   { In due time, one will be allocated appropriately.        }
  882.   AttrWord := DDL_READWRITE;
  883.   if HandleAllocated then
  884.   begin
  885.     { Set attribute flags based on values in FileType }
  886.     for AttrIndex := ftReadOnly to ftArchive do
  887.      if AttrIndex in FileType then
  888.       AttrWord := AttrWord or Attributes[ AttrIndex ];
  889.  
  890.     { Use Exclusive bit to exclude normal files }
  891.     if not ( ftNormal in FileType ) then
  892.       AttrWord := AttrWord or DDL_EXCLUSIVE;
  893.  
  894.     ChDir( FDirectory ); { go to the directory we want }
  895.     Clear;               { clear the list }
  896.     GetMem( MaskPtr , 256 );
  897.     StrPCopy( MaskPtr , FMask );
  898.     while MaskPtr <> nil do
  899.     begin
  900.       Ptr := StrScan ( MaskPtr , ';' );
  901.       if Ptr <> nil then  Ptr^ := #0;
  902.       { build the list }
  903.       SendMessage( Handle , LB_DIR , AttrWord , Longint( MaskPtr ));
  904.       if Ptr <> nil then
  905.       begin
  906.         Ptr^ := ';';
  907.         Inc ( Ptr );
  908.       end;
  909.       MaskPtr := Ptr;
  910.     end;
  911.     FreeMem( MaskPtr , 256 );
  912.     { Now add the bitmaps }
  913.     {---------------------------- begin custom code --------------------------}
  914.     { Create the TPicture for exchange purposes }
  915.     TempPicture := TPicture.Create;
  916.     { Set it to icon widths }
  917.     TempPicture.Bitmap.Width := 32;
  918.     TempPicture.Bitmap.Height := 32;
  919.     { Run down the list }
  920.     for i := 0 to Items.Count - 1 do
  921.     begin
  922.       { Create a new temporary icon }
  923.       TempIcon := TIcon.Create;
  924.       { Call the custom DRWS routine to get icon for a file }
  925.       GetIconForFile( Items[ i ] , TempIcon );
  926.       { Put the icon on the bitmap for the picture via draw }
  927.       { Note 1 , 1 due to bug in Draw?                      }
  928.       TempPicture.Bitmap.Canvas.Draw( 1 , 1 , TempIcon );
  929.       { Create a temporary bitmap }
  930.       TempBmp := TBitmap.Create;
  931.       { Set its width to those of the previous object's bitmaps }
  932.       TempBmp.Width := 16;
  933.       TempBmp.Height := 15;
  934.       { Resize the icon's bitmap to the smaller size with stretchdraw }
  935.       TempBmp.Canvas.StretchDraw( Rect( 1 , 1 , 15 , 14 ) ,
  936.        TempPicture.Bitmap );
  937.       { Set the Objects list to the bitmap }
  938.       Items.Objects[ i ] := TempBmp;
  939.       { Free the icon each iteration; don't free the TempBmp as list does }
  940.       TempIcon.Free;
  941.     end;
  942.     { Free the TPicture exchange element }
  943.     TempPicture.Free;
  944.     {------------------------ end custom code --------------------------------}
  945.     Change;
  946.   end;
  947. end;
  948.  
  949. { Use this to respond to dbl-clicking FLB filename }
  950. procedure TIconFileListBox.TheDblClick(Sender: TObject);
  951. begin
  952.   { Call shellexec as a wrapper around ShellExecute API call }
  953.   { False indicates failure, signal error                    }
  954.   if not ShellExec( ExpandFileName( Items[ ItemIndex ] ), '' , '', false ,
  955.    SW_SHOWNORMAL , false ) then MessageDlg('Could not Shell out to ' +
  956.     Items[ ItemIndex ] , mtError, [mbOK], 0);
  957. end;
  958.  
  959. { Create method for FIP                                }
  960. constructor TIconFileListBox.Create( AOwner : TComponent );
  961. begin
  962.   { call inherited -- VITAL! }
  963.   inherited Create( AOwner );
  964.   { set the mouse method }
  965.   OnDblClick := TheDblClick;
  966. end;
  967.  
  968. { Create method for FIP                                }
  969. constructor TFileIconPanel.Create( AOwner : TComponent );
  970. begin
  971.   { call inherited -- VITAL! }
  972.   inherited Create( AOwner );
  973.   { create icon and label components, making self owner/displayer }
  974.   FTheIcon := TIcon.Create;
  975.   FTheLabel := TLabel.Create( Self );
  976.   FThelabel.Parent := Self;
  977.   { Set own and labels mouse methods to stored methods }
  978.   OnClick := TheClick;
  979.   OnDblClick := TheDblClick;
  980.   OnMouseUp := TheMouseUp;
  981.   OnMouseDown := TheMouseDown;
  982.   FTheLabel.OnClick := TheClick;
  983.   FTheLabel.OnDblClick := TheDblClick;
  984.   FTheLabel.OnMouseUp := TheMouseUp;
  985.   FTheLabel.OnMouseDown := TheMouseDown;
  986.   { Set alignment and autosize properties of the label }
  987.   FTheLabel.Autosize := false;
  988.   FTheLabel.Alignment := taCenter;
  989.   { Set selected to false }
  990.   Selected := false;
  991. end;
  992.  
  993. { Initialization method for FIP                                         }
  994. procedure TFileIconPanel.Initialize( PanelX              ,
  995.                                      PanelY              ,
  996.                                      PanelWidth          ,
  997.                                      PanelHeight         ,
  998.                                      PanelBevelWidth     ,
  999.                                      LabelFontSize         : Integer;
  1000.                                      PanelColor          ,
  1001.                                      PanelHighlightColor ,
  1002.                                      PanelShadowColor    ,
  1003.                                      LabelTextColor        : TColor;
  1004.                                      TheFilename         ,
  1005.                                      LabelFontName         : String;
  1006.                                      LabelFontStyle        : TFontStyles;
  1007.                                      ExtraData             : Integer );
  1008.  
  1009. var TheLabelHeight ,             { Holder for label pixel height }
  1010.     TheLabelWidth    : Integer;  { Holder for label pixel width  }
  1011.     TheOtherPChar    : PChar;    { Windows ASCIIZ string         }
  1012. begin
  1013.   { Set the basic properties based on imported parameters }
  1014.   Left := PanelX;
  1015.   Top := PanelY;
  1016.   Width := PanelWidth;
  1017.   Height := PanelHeight;
  1018.   Color := PanelColor;
  1019.   BevelWidth := PanelBevelWidth;
  1020.   FHighlightColor := PanelHighlightColor;
  1021.   FShadowColor := PanelShadowColor;
  1022.   FTheName := TheFilename;
  1023.   { If the ExtraData field is non-0 then a drive is being sent in }
  1024.   if ExtraData <> 0 then
  1025.   begin
  1026.     { Use the data field value to determine which icon to get from RES file }
  1027.     case ExtraData of
  1028.       1 : begin
  1029.             GetMem( TheOtherPChar , 255 );
  1030.             StrPCopy( TheOtherPChar , 'FLOPPY35' );
  1031.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  1032.             FreeMem( TheOtherPChar , 255 );
  1033.           end;
  1034.       2 : begin
  1035.             GetMem( TheOtherPChar , 255 );
  1036.             StrPCopy( TheOtherPChar , 'FIXEDHD' );
  1037.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  1038.             FreeMem( TheOtherPChar , 255 );
  1039.           end;
  1040.       3 : begin
  1041.             GetMem( TheOtherPChar , 255 );
  1042.             StrPCopy( TheOtherPChar , 'NETWORKHD' );
  1043.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  1044.             FreeMem( TheOtherPChar , 255 );
  1045.           end;
  1046.       4 : begin
  1047.             GetMem( TheOtherPChar , 255 );
  1048.             StrPCopy( TheOtherPChar , 'CDROM' );
  1049.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  1050.             FreeMem( TheOtherPChar , 255 );
  1051.           end;
  1052.       5 : begin
  1053.             GetMem( TheOtherPChar , 255 );
  1054.             StrPCopy( TheOtherPChar , 'RAM' );
  1055.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  1056.             FreeMem( TheOtherPChar , 255 );
  1057.           end;
  1058.     end;
  1059.     { The FileNme property is already set up for the caption; use directly }
  1060.     FTheLabel.Caption := TheFilename;
  1061.     { Set up the hint for later use (make sure to set ShowHint) }
  1062.     Hint := 'Change to ' + TheFileName;
  1063.     ShowHint := true;
  1064.     { Set up all imported label properties and center it for drawing }
  1065.     with FTheLabel do
  1066.     begin
  1067.       Font.Name := LabelFontName;
  1068.       Font.Size := LabelFontSize;
  1069.       Font.Style := LabelFontStyle;
  1070.       Font.Color := LabelTextColor;
  1071.       Canvas.Brush.Color := PanelColor;
  1072.       Canvas.Font := Font;
  1073.       TheLabelHeight := Canvas.Textheight( Caption ) + 4;
  1074.       TheLabelWidth := Canvas.Textwidth( Caption ) + 4;
  1075.       Left := (( Self.Width - TheLabelWidth ) div 2 ) + 1;
  1076.       Top := ((( Round( Self.Height * 0.25 ) - 6 ) - TheLabelHeight) div 2) + 1;
  1077.       Top := Top + Round( Self.Height * 0.75 );
  1078.       Height := TheLabelHeight;
  1079.       Width := TheLabelWidth;
  1080.     end;
  1081.   end
  1082.   else
  1083.   begin
  1084.     { A file or directory has been sent in; use GetIconForFile to obtain an }
  1085.     { icon either from the file, its owner, or a RES file default.          }
  1086.     GetIconForFile( FTheName , FTheIcon );
  1087.     { Check for the Backup caption and set it specially }
  1088.     if ExtractfileName( FThename ) = '..' then
  1089.     begin
  1090.       FTheLabel.Caption := '..';
  1091.       Hint := 'Up One Level';
  1092.     end
  1093.     else
  1094.     begin
  1095.       { Otherwise just get the filename for the label caption }
  1096.       { And the full path for the hint (used later.)          }
  1097.       FTheLabel.caption := ExtractFileName( UpperCase( FTheName ));
  1098.       Hint := FTheName;
  1099.     end;
  1100.     { Activate showhint so hints are seen }
  1101.     ShowHint := true;
  1102.     { Set label properties with imported values and center for display }
  1103.     with FTheLabel do
  1104.     begin
  1105.       Font.Name := LabelFontName;
  1106.       Font.Size := LabelFontSize;
  1107.       Font.Style := LabelFontStyle;
  1108.       Font.Color := LabelTextColor;
  1109.       Canvas.Brush.Color := PanelColor;
  1110.       Canvas.Font := Font;
  1111.       TheLabelHeight := Canvas.Textheight( Caption ) + 4;
  1112.       TheLabelWidth := Canvas.Textwidth( Caption ) + 4;
  1113.       Left := (( Self.Width - TheLabelWidth ) div 2 ) + 1;
  1114.       Top := ((( Round( Self.Height * 0.25 ) - 6 ) - TheLabelHeight) div 2) + 1;
  1115.       Top := Top + Round( Self.Height * 0.75 );
  1116.       Height := TheLabelHeight;
  1117.       Width := TheLabelWidth;
  1118.     end;
  1119.   end;
  1120. end;
  1121.  
  1122. { Destroy method for FIP }
  1123. destructor TFileIconPanel.Destroy;
  1124. begin
  1125.   { free component resources }
  1126.   FTheIcon.Free;
  1127.   FTheLabel.Free;
  1128.   { call inherited -- VITAL! }
  1129.   inherited Destroy;
  1130. end;
  1131.  
  1132. { Mousedown method for FIP; used to allow dragging }
  1133. procedure TFileIconPanel.TheMouseDown(Sender: TObject;
  1134.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  1135. begin
  1136.   { Begin a conditional drag operation (false allows timer) }
  1137.   BeginDrag( false );
  1138. end;
  1139.  
  1140. { Mouseup Method for FIP; used to allow dragging }
  1141. procedure TFileIconPanel.TheMouseUp(Sender: TObject;
  1142.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  1143. begin
  1144.   { End a drag operation without dropping; if dragged OK }
  1145.   { already handled.                                     }
  1146.   {EndDrag( false );}
  1147. end;
  1148.  
  1149. { TheClick method for FIP; used for event responses }
  1150. procedure TFileIconPanel.TheClick( Sender : TObject );
  1151. begin
  1152.   { Currently ignore drive clicks }
  1153.   if Pos( 'DRIVE' , FTheName ) > 0 then exit;
  1154.   { Flip status of bevels }
  1155.   if BevelOuter = bvRaised then BevelOuter := bvLowered else
  1156.    BevelOuter := bvRaised;
  1157.   { Flip selected variable }
  1158.   Selected := not Selected;
  1159.   { Set redisplay }
  1160.   Invalidate;
  1161. end;
  1162.  
  1163. { TheDblClick method for FIP; used for event responses }
  1164. procedure TFileIconPanel.TheDblClick( Sender : TObject );
  1165. var CurrentDirectory : String;    { Use to store dirs }
  1166.     TheDrive         : String;    { Get drive letter  }
  1167.     WhichDrive       : Integer;   { Get drive number  }
  1168.     ErrorCheck       : Integer;
  1169.     TheFWB           : TFileWorkBench;
  1170. begin
  1171.   { Create FileWorkBench for later use }
  1172.   TheFWB := TFileWorkBench.Create( Self );
  1173.   { Check for label or FIP sender }
  1174.   if Sender is TFileIconPanel then
  1175.   begin
  1176.     if FTheLabel.Caption = '..' then
  1177.     begin { deal with backup request }
  1178.       { Change to new directory }
  1179.       TheFWB.ChangeTheDirectory( '..' );
  1180.       { Call special method due to SendMessage problem! }
  1181.       TFileIconPanelScrollBox( Parent ).Update;
  1182.     end
  1183.     else
  1184.     begin
  1185.       { Check for DRIVE id in name }
  1186.       if Pos( 'DRIVE' , FTheName ) <> 0 then
  1187.       begin { Double Click on a Drive Icon }
  1188.         { Pull out the letter from name }
  1189.         TheDrive := Copy( FtheName , 7 , 1 );
  1190.         { Convert it to a number }
  1191.         WhichDrive := ( Ord( TheDrive[ 1 ] ) - Ord( 'A' )) + 1;
  1192.         TheFWB.ChangeTheDriveAndDirectory( WhichDrive );
  1193.         { Call special method due to SendMessage problem! }
  1194.         TFileIconPanelScrollBox( Parent ).Update;
  1195.       end
  1196.       else
  1197.       begin { Double click on a dir/file icon }
  1198.         if (( FileGetAttr( FTheName ) and faDirectory ) = faDirectory ) then
  1199.         begin { A directory, change to it }
  1200.           { Since full path in name, simply change to it! }
  1201.           TheFWB.ChangeTheDirectory( FTheName );
  1202.           { Call special method due to SendMessage problem! }
  1203.           TFileIconPanelScrollBox( Parent ).Update;
  1204.         end
  1205.         else
  1206.         begin { A file; attempt to shellexecute it }
  1207.           { Call shellexec as a wrapper around ShellExecute API call }
  1208.           { False indicates failure, signal error                    }
  1209.           if not ShellExec( FTheName , '' , '', false , SW_SHOWNORMAL , false )
  1210.            then MessageDlg('Could not Shell out to ' + FTheName , mtError,
  1211.             [mbOK], 0);
  1212.         end;
  1213.       end;
  1214.     end;
  1215.   end
  1216.   else
  1217.   begin
  1218.     with Sender as TLabel do
  1219.     begin
  1220.       if Caption = '..' then
  1221.       begin { Deal with backup request }
  1222.         { Change to new directory }
  1223.         TheFWB.ChangeTheDirectory( '..' );
  1224.         { Call special method due to SendMessage problem! }
  1225.         TFileIconPanelScrollBox( Parent ).Update;
  1226.       end
  1227.       else
  1228.       begin
  1229.         with Parent as TFileIconPanel do
  1230.         begin
  1231.           { Check for DRIVE id in name }
  1232.           if Pos( 'DRIVE' , FTheName ) <> 0 then
  1233.           begin { Double Click on a Drive Icon }
  1234.             { Pull out the letter from name }
  1235.             TheDrive := Copy( FtheName , 7 , 1 );
  1236.             { Convert it to a number }
  1237.             WhichDrive := ( Ord( TheDrive[ 1 ] ) - Ord( 'A' )) + 1;
  1238.             { Call the method to change to default dir on new drive }
  1239.             TheFWB.ChangeTheDriveAndDirectory( WhichDrive );
  1240.             { Call special method due to SendMessage problem! }
  1241.             TFileIconPanelScrollBox( Parent ).Update;
  1242.           end
  1243.           else
  1244.           begin { Double click on a dir/file icon }
  1245.             if (( FileGetAttr( FTheName ) and faDirectory ) = faDirectory ) then
  1246.             begin { A directory, change to it }
  1247.               { Since full path in name, simply change to it! }
  1248.               TheFWB.ChangeTheDirectory( FTheName );
  1249.               { Call special method due to SendMessage problem! }
  1250.               TFileIconPanelScrollBox( Parent ).Update;
  1251.             end
  1252.             else
  1253.             begin { A file; attempt to shellexecute it }
  1254.               { Call shellexec as a wrapper around ShellExecute API call }
  1255.               { False indicates failure, signal error                    }
  1256.               if not ShellExec( FTheName , '' , '', false , SW_SHOWNORMAL ,
  1257.                false ) then MessageDlg('Could not Shell out to ' + FTheName ,
  1258.                 mtError, [mbOK], 0);
  1259.             end;
  1260.           end;
  1261.         end;
  1262.       end;
  1263.     end;
  1264.   end;
  1265.   TheFWB.Free; { This prevents resource leak }
  1266. end;
  1267.  
  1268. { Paint method for FIP; overrides normal paint }
  1269. procedure TFileIconPanel.Paint;
  1270. var
  1271.   TheOtherRect   : TRect;   { Holds clientrect   }
  1272.   TopColor     ,            { Holds bright color }
  1273.   BottomColor    : TColor;  { Holds dark color   }
  1274.  
  1275. { These methods are from Borland Intl., copyright 1995 }
  1276. procedure Frame3D(    Canvas       : TCanvas;
  1277.                   var TheRect      : TRect;
  1278.                       TopColor   ,
  1279.                       BottomColor  : TColor;
  1280.                       Width        : Integer );
  1281.  
  1282. procedure DoRect;
  1283. var
  1284.   TopRight, BottomLeft: TPoint;
  1285. begin
  1286.   with Canvas, TheRect do
  1287.   begin
  1288.     TopRight.X := Right;
  1289.     TopRight.Y := Top;
  1290.     BottomLeft.X := Left;
  1291.     BottomLeft.Y := Bottom;
  1292.     Pen.Color := TopColor;
  1293.     PolyLine([BottomLeft, TopLeft, TopRight]);
  1294.     Pen.Color := BottomColor;
  1295.     Dec(BottomLeft.X);
  1296.     PolyLine([TopRight, BottomRight, BottomLeft]);
  1297.   end;
  1298. end;
  1299.  
  1300. begin
  1301.   Canvas.Pen.Width := 1;
  1302.   Dec(TheRect.Bottom); Dec(TheRect.Right);
  1303.   while Width > 0 do
  1304.   begin
  1305.     Dec(Width);
  1306.     DoRect;
  1307.     InflateRect(TheRect, -1, -1);
  1308.   end;
  1309.   Inc(TheRect.Bottom); Inc(TheRect.Right);
  1310. end;
  1311.  
  1312. procedure AdjustColors(Bevel: TPanelBevel);
  1313. begin
  1314.   TopColor := FHighlightColor;
  1315.   if Bevel = bvLowered then TopColor := FShadowColor;
  1316.   BottomColor := FShadowColor;
  1317.   if Bevel = bvLowered then BottomColor := FHighlightColor;
  1318. end;
  1319.  
  1320. { Custom code begins here }
  1321. begin
  1322.   { Get the rectangle of the control with API/method call }
  1323.   TheOtherRect := GetClientRect;
  1324.   { draw basic rectangle with basic color }
  1325.   with Canvas do
  1326.   begin
  1327.     Brush.Color := Color;
  1328.     FillRect(TheOtherRect);
  1329.   end;
  1330.   { Set up for top "icon" frame  and draw it with frame3d }
  1331.   TheOtherRect.Right := Width;
  1332.   TheOtherRect.Bottom := Round( Height * 0.75 ) - 6 ;
  1333.   if BevelOuter <> bvNone then
  1334.   begin
  1335.     AdjustColors(BevelOuter);
  1336.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  1337.   end;
  1338.   Frame3D(Canvas, TheOtherRect, Color, Color, BorderWidth);
  1339.   if BevelInner <> bvNone then
  1340.   begin
  1341.     AdjustColors(BevelInner);
  1342.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  1343.   end;
  1344.   { Do the same for the lower "label" frame }
  1345.   TheOtherRect.Top := Round( Height * 0.75 ) - 5;
  1346.   TheOtherRect.Left := 0;
  1347.   TheOtherRect.Bottom := Height;
  1348.   TheOtherRect.Right := Width;
  1349.   if BevelOuter <> bvNone then
  1350.   begin
  1351.     AdjustColors(BevelOuter);
  1352.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  1353.   end;
  1354.   Frame3D(Canvas, TheOtherRect, Color, Color, BorderWidth);
  1355.   if BevelInner <> bvNone then
  1356.   begin
  1357.     AdjustColors(BevelInner);
  1358.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  1359.   end;
  1360.   { Then draw the icon using canvas draw method }
  1361.   Canvas.Draw( (( Width - 32 ) div 2 ) + 1 ,
  1362.   ((( Round( Height * 0.75 ) - 6 ) - 32 ) div 2 ) + 1 , FTheIcon );
  1363. end;
  1364.  
  1365. { This procedure clears a scrollbox of all FileIconPanels }
  1366. procedure TFileIconPanelScrollbox.ClearTheFIPs;
  1367. var Counter_1 : Integer;
  1368.     TheComponent : TComponent;
  1369. begin
  1370.   { Note that must use while loop since component count continually }
  1371.   { decreases as removes are made!                                  }
  1372.   while ComponentCount > 0 do
  1373.   begin
  1374.     { Save the component as a generic TComponent }
  1375.     TheComponent := Components[ 0 ];
  1376.     { Call removecomponent to pull it out of the owner list for sb }
  1377.     { This avoids GPF when freeing the sb.                         }
  1378.     RemoveComponent( Components[ 0 ]);
  1379.     if ControlCount > 0 then
  1380.      RemoveControl( Controls[ 0 ] );
  1381.     { Typecast the pointer and free it to release memory and res. }
  1382.     TFileIconPanel( TheComponent ).visible := false;
  1383.     Application.MainForm.InsertComponent( TheComponent );
  1384.   end;
  1385. end;
  1386.  
  1387. { This procedure scans for drives and obtains their type and creates file }
  1388. { icon panels to represent them.                                          }
  1389. procedure TFileIconPanelScrollBox.AddDriveIcons( var XCounter ,
  1390.            YCounter : Integer );
  1391. type
  1392.   { This if from filectrl unit; reproduce here for completeness }
  1393.   TDriveType = (dtUnknown, dtNoDrive, dtFloppy, dtFixed, dtNetwork, dtCDROM,
  1394.                 dtRAM);
  1395. var
  1396.   DrivePC         : array[0..256] of char;
  1397.   DriveNum        : Integer;         { Used to get next drive via DOS fn   }
  1398.   IconType        : Integer;         { Used to hold icon type (defacto dt) }
  1399.   DriveChar       : Char;            { Used to hold drive letter           }
  1400.   DriveType       : TDriveType;      { Used for set-valued drive type      }
  1401.   Finished        : Boolean;         { Loop flag                           }
  1402.   TheFIP          : TFileIconPanel;  { Generic FileIconPanel variable      }
  1403.   ButtonColor   ,                    { Main panel color                    }
  1404.   ButtonHLColor ,                    { Bright panel color                  }
  1405.   ButtonSColor  ,                    { Dark panel color                    }
  1406.   Textcolor       : TColor;          { Label text color                    }
  1407.  
  1408. { This code is from the FileCtrl Unit; copyright Borland Intl 1995      }
  1409. { Check whether drive is a CD-ROM.  Returns True if MSCDEX is installed }
  1410. {  and the drive is using a CD driver                                   }
  1411.  
  1412. (*function IsCDROM(DriveNum: Integer): Boolean; assembler;
  1413. asm
  1414.   MOV   AX,1500h { look for MSCDEX }
  1415.   XOR   BX,BX
  1416.   INT   2fh
  1417.   OR    BX,BX
  1418.   JZ    @Finish
  1419.   MOV   AX,150Bh { check for using CD driver }
  1420.   MOV   CX,DriveNum
  1421.   INT   2fh
  1422.   OR    AX,AX
  1423.   @Finish:
  1424. end;
  1425.  
  1426. { This code is from the FileCtrl Unit; copyright Borland Intl 1995      }
  1427. { Check whether drive is a RAM drive.                                   }
  1428. function IsRAMDrive(DriveNum: Integer): Boolean; assembler;
  1429. var
  1430.   TempResult: Boolean;
  1431. asm
  1432.   MOV   TempResult,False
  1433.   PUSH  DS
  1434.   MOV   BX,SS
  1435.   MOV   DS,BX
  1436.   SUB   SP,0200h
  1437.   MOV   BX,SP
  1438.   MOV   AX,DriveNum
  1439.   MOV   CX,1
  1440.   XOR   DX,DX
  1441.   INT   25h  { read boot sector }
  1442.   ADD   SP,2
  1443.   JC    @ItsNot
  1444.   MOV   BX,SP
  1445.   CMP   BYTE PTR SS:[BX+15h],0F8h  { reverify fixed disk }
  1446.   JNE   @ItsNot
  1447.   CMP   BYTE PTR SS:[BX+10h],1  { check for single FAT }
  1448.   JNE   @ItsNot
  1449.   MOV   TempResult,True
  1450.   @ItsNot:
  1451.   ADD   SP,0200h
  1452.   POP   DS
  1453.   MOV   AL, TempResult
  1454. end;
  1455.  
  1456. { This code is from the FileCtrl Unit; copyright Borland Intl 1995      }
  1457. { Finds the type of a drive letter.                                     }
  1458. function FindDriveType(DriveNum: Integer): TDriveType;
  1459. begin
  1460.   Result := TDriveType(GetDriveType(DriveNum));
  1461.   if (Result = dtFixed) or (Result = dtNetwork) then
  1462.   begin
  1463.     if IsCDROM(DriveNum) then Result := dtCDROM
  1464.     else if (Result = dtFixed) then
  1465.     begin
  1466.         { do not check for RAMDrive under Windows NT }
  1467.       if ((GetWinFlags and $4000) = 0) and IsRAMDrive(DriveNum) then
  1468.         Result := dtRAM;
  1469.     end;
  1470.   end;
  1471. end;*)
  1472.  
  1473. begin
  1474.   { Set the button colors to an aquamarine color scheme for drives }
  1475.   ButtonColor := clTeal;
  1476.   ButtonHLColor := clAqua;
  1477.   ButtonSColor := clNavy;
  1478.   TextColor := clblack;
  1479.   { Set initial variables before looping for all drives }
  1480.   finished := false;
  1481.   DriveNum := 0;
  1482.   while not finished do
  1483.   begin
  1484.     { Start with no drive found }
  1485.     IconType := 0;
  1486.     (*=============REMOVED DUE TO WINDOWS 95=========
  1487.     { Call the Borland method to get the drive info }
  1488.     DriveType := FindDriveType(DriveNum);
  1489.     ===============END WINDOWS 95 REMOVAL==========*)
  1490.     { Set its letter and make it uppercase }
  1491.     DriveChar := Chr(DriveNum + ord('a'));
  1492.     DriveChar := Upcase(DriveChar);
  1493.     StrPCopy( DrivePC , DriveChar + ':\' );
  1494.     {*&&&&&&&&&&&&&&&  WIN 95 CALL  &&&&&&&&&&&&&&&&&&&*}
  1495.     DriveType := TDriveType(GetDriveType( DrivePC ));
  1496.     { Assign an icon based on the drive type; if no drive exists type is nil }
  1497.     case DriveType of
  1498.       dtFloppy  : IconType := 1;
  1499.       dtFixed   : IconType := 2;
  1500.       dtNetwork : IconType := 3;
  1501.       dtCDROM   : IconType := 4;
  1502.       dtRAM     : IconType := 5;
  1503.     end;
  1504.     { Set to check next drive letter }
  1505.     DriveNum := DriveNum + 1;
  1506.     { But if no match then out of drives so set exit flag }
  1507.     if IconType = 0 then finished := true;
  1508.     { If drive was valid then set up the new FileIconPanel on the imported }
  1509.     { Scrollbox                                                            }
  1510.     if not finished then
  1511.     begin
  1512.       { Create the FileIconPanel and set its parent for memory mgmt and display}
  1513.       TheFIP := TFileIconPanel.Create( Self );
  1514.       TheFIP.Parent := Self;
  1515.       { Call its initialize method with imported position values and the   }
  1516.       { preset color scheme, a drive caption, and a minimum font. Note the }
  1517.       { setting of the ExtraData field to non-zero; this signals a drive   }
  1518.       { rather than a file being sent in.                                  }
  1519.       TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1520.        (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize , 3 ,
  1521.         7 , ButtonColor, ButtonHLColor,
  1522.        ButtonSColor , TextColor , 'DRIVE ' + DriveChar + ':' , 'MS Serif' , [] ,
  1523.        IconType );
  1524.       { Increment the column counter; if it exceeds max move to new row      }
  1525.       { Note that these are 'var' parameters and will export final position. }
  1526.       XCounter := XCounter + 1;
  1527.       if XCounter > MaxIconsInARow then
  1528.       begin
  1529.         XCounter := 1;
  1530.         YCounter := YCounter + 1;
  1531.       end;
  1532.     end;
  1533.   end;
  1534. end;
  1535.  
  1536. { This procedure assigns colors to FIP's based on file attributes }
  1537. procedure TFileIconPanelScrollBox.GetColorsForFileIcon( TheFile : String;
  1538.            var BC , HC , SC , TC : TColor );
  1539. var AmADir      ,             { Booleans hold file attribs }
  1540.     AmAnArchive ,
  1541.     AmAVolumeId ,
  1542.     AmHidden    ,
  1543.     AmReadOnly  ,
  1544.     AmSystem      : Boolean;
  1545. begin
  1546.   { Make the call to internal fileworkbench to set attributes }
  1547.   TheFWB.GetFileAttributes( TheFile , AmADir , AmAnArchive , AmAVolumeId ,
  1548.    AmHidden , AmReadOnly , AmSystem );
  1549.   { Volume ID has no subtypes }
  1550.   if AmAVolumeID then
  1551.   begin
  1552.     BC := clOlive;
  1553.     HC := clYellow;
  1554.     SC := clBlack;
  1555.     TC := clWhite;
  1556.     exit;
  1557.   end;
  1558.   { Check all directory combinations }
  1559.   if AmADir then
  1560.   begin
  1561.     BC := clNavy;
  1562.     HC := clBlue;
  1563.     SC := clBlack;
  1564.     TC := clWhite;
  1565.     if AmHidden then
  1566.     begin
  1567.       if AmReadOnly then
  1568.       begin
  1569.         if AmSystem then
  1570.         begin { One HECK of a file! }
  1571.           BC := clBlack;
  1572.           HC := clSilver;
  1573.           SC := clGray;
  1574.           TC := clWhite;
  1575.         end
  1576.         else
  1577.         begin { Dir,RO,Hid }
  1578.           BC := clMaroon;
  1579.           HC := clFuchsia;
  1580.           SC := clGreen;
  1581.           TC := clWhite;
  1582.         end;
  1583.       end
  1584.       else
  1585.       begin { Dir,Hid }
  1586.         BC := clPurple;
  1587.         HC := clFuchsia;
  1588.         SC := clBlack;
  1589.         TC := clWhite;
  1590.       end;
  1591.     end
  1592.     else
  1593.     begin
  1594.       if AmReadOnly then
  1595.       begin
  1596.         if AmSystem then
  1597.         begin { Dir,RO,Sys }
  1598.           BC := clMaroon;
  1599.           HC := clLime;
  1600.           SC := clGreen;
  1601.           TC := clWhite;
  1602.         end
  1603.         else
  1604.         begin { Dir,RO }
  1605.           BC := clGreen;
  1606.           HC := clLime;
  1607.           SC := clBlack;
  1608.           TC := clWhite;
  1609.         end;
  1610.       end
  1611.       else
  1612.       begin
  1613.         if AmSystem then
  1614.         begin { Dir,Sys }
  1615.           BC := clMaroon;
  1616.           HC := clRed;
  1617.           SC := clBlack;
  1618.           TC := clWhite;
  1619.         end;
  1620.       end;
  1621.     end;
  1622.   end
  1623.   else { Archive Only; check all combinations }
  1624.   begin
  1625.     BC := clSilver;
  1626.     HC := clWhite;
  1627.     SC := clGray;
  1628.     TC := clBlack;
  1629.     if AmHidden then
  1630.     begin
  1631.       if AmReadOnly then
  1632.       begin
  1633.         if AmSystem then
  1634.         begin { Hid,RO,Sys }
  1635.           BC := clRed;
  1636.           HC := clLime;
  1637.           SC := clPurple;
  1638.           TC := clBlack;
  1639.         end
  1640.         else
  1641.         begin { RO,Hid }
  1642.           BC := clLime;
  1643.           HC := clFuchsia;
  1644.           SC := clMaroon;
  1645.           TC := clBlack;
  1646.         end;
  1647.       end
  1648.       else
  1649.       begin { Hid }
  1650.         BC := clFuchsia;
  1651.         HC := clWhite;
  1652.         SC := clPurple;
  1653.         TC := clBlack;
  1654.       end;
  1655.     end
  1656.     else
  1657.     begin
  1658.       if AmReadOnly then
  1659.       begin
  1660.         if AmSystem then
  1661.         begin { RO,Sys }
  1662.           BC := clRed;
  1663.           HC := clLime;
  1664.           SC := clMaroon;
  1665.           TC := clBlack;
  1666.         end
  1667.         else
  1668.         begin { RO }
  1669.           BC := clLime;
  1670.           HC := clWhite;
  1671.           SC := clGreen;
  1672.           TC := clBlack;
  1673.         end;
  1674.       end
  1675.       else
  1676.       begin
  1677.         if AmSystem then
  1678.         begin { System }
  1679.           BC := clRed;
  1680.           HC := clWhite;
  1681.           SC := clMaroon;
  1682.           TC := clBlack;
  1683.         end;
  1684.       end;
  1685.     end;
  1686.   end;
  1687. end;
  1688.  
  1689. { This procedure gets all icons for an given directory, including drives and }
  1690. { standard subdirectories. It does not get special combinations or h/ro/sys  }
  1691. procedure TFileIconPanelScrollbox.GetIconsForEntireDirectory(
  1692.             TargetPath  : String );
  1693. var Finished        : Boolean;         { Loop flag              }
  1694.     TheSR           : TSearchRec;      { Searchrecord for FF/FN }
  1695.     TheResult       : Integer;         { return variable        }
  1696.     TempPath        : String;          { path for FF/FN         }
  1697.     TheFIP          : TFileIconPanel;  { generic FIP holder     }
  1698.     RowCounter    ,                    { position in row of FIP }
  1699.     ColumnCounter   : Integer;         { position in col of FIP }
  1700.     ButtonColor   ,                    { main panel color       }
  1701.     ButtonHLColor ,                    { bright panel color     }
  1702.     ButtonSColor  ,                    { dark panel color       }
  1703.     Textcolor       : TColor;          { label text color       }
  1704.     IsADir ,                           { Variable for file attr }
  1705.     IsAnArchive ,
  1706.     IsAVolumeID,
  1707.     IsAReadOnlyFile,
  1708.     IsAHiddenFile ,
  1709.     IsASystemFile     : Boolean;
  1710.     MaxTextLength     : Integer;       { Used to safely set size}
  1711. begin
  1712.   { hide during refresh }
  1713.   Visible := false;
  1714.   { Get the icon sizes }
  1715.   TheFIP := TFileIconPanel.Create( Self );
  1716.   TheFIP.Parent := Self;
  1717.   TheFIP.FTheLabel.Canvas.Font.Name := 'MS Serif';
  1718.   TheFIP.FTheLabel.Canvas.Font.Size := 7;
  1719.   MaxTextLength := TheFIP.FTheLabel.Canvas.TextWidth( 'COMMAND.COM' );
  1720.   TheFIP.Free;
  1721.   TheIconSize := MaxTextLength + 13;
  1722.   TheIconSpacing := TheIconSize + 5;
  1723.   { Set up maximum icons per row based on screen size }
  1724.   MaxIconsInARow := ( Screen.Width div TheIconSpacing );
  1725.   { Set up the position counters }
  1726.   RowCounter := 1;
  1727.   ColumnCounter := 1;
  1728.   { Get the drives for the current machine }
  1729.   AddDriveIcons( ColumnCounter , RowCounter  );
  1730.   { Set up the initial variables }
  1731.   Finished := false;
  1732.   TempPath := TargetPath + '*.*';
  1733.   { Make the call to FindFirst set to get any file; will return '.' }
  1734.   { so discard it.                                                  }
  1735.   TheResult := FindFirst( TempPath , faAnyFile , TheSR );
  1736.   { loop through all files in the directory and look for directories }
  1737.   while not Finished do
  1738.   begin
  1739.     { Make call to FindNext, using only SearchRecord from FindFirst }
  1740.     TheResult := FindNext( TheSR );
  1741.     { A -1 result means no more files so exit }
  1742.     if TheResult <> 0 then finished := true else
  1743.     begin
  1744.       { Otherwise check for a directory attribute }
  1745.       if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory ) =
  1746.        faDirectory ) then
  1747.       begin
  1748.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1749.          ButtonHLColor , ButtonSColor , TextColor );
  1750.         { If found create a new FileIconPanel on the imported scrollbox }
  1751.         { Note sending 0 ExtraData parameter to indicate file not drive }
  1752.         TheFIP := TFileIconPanel.Create( Self );
  1753.         TheFIP.Parent := Self;
  1754.         TheFIP.Initialize((( ColumnCounter - 1 ) * TheIconSpacing ),
  1755.          (( RowCounter - 1  ) * TheIconSpacing ) , TheIconSize, TheIconSize ,
  1756.           3 , 7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1757.            TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  1758.         { Increment column counter and move to new row if past limit }
  1759.         ColumnCounter := ColumnCounter + 1;
  1760.         if ColumnCounter > MaxIconsInARow then
  1761.         begin
  1762.           ColumnCounter := 1;
  1763.           RowCounter := RowCounter + 1;
  1764.         end;
  1765.       end;
  1766.     end;
  1767.   end;
  1768.   { Set up new initialization variables }
  1769.   Finished := false;
  1770.   TempPath := TargetPath + '*.*';
  1771.   { Make needed call to FindFirst and discard '.' }
  1772.   TheResult := FindFirst( TempPath , faAnyFile , TheSR );
  1773.   while not Finished do
  1774.   begin
  1775.     { Loop through file again, this time getting only archive files }
  1776.     TheResult := FindNext( TheSR );
  1777.     { Result of -1 indicates no more files }
  1778.     if TheResult <> 0 then Finished := true else
  1779.     begin
  1780.       { If faArchive file then add new FileIconPanel }
  1781.       TheFWB.GetFileAttributes(( Targetpath + TheSR.Name ) , IsADir ,
  1782.        IsAnArchive , IsAVolumeId , IsAHiddenFile , IsAReadOnlyFile ,
  1783.         IsASystemFile );
  1784.       if (( IsAnArchive ) and ( not IsADir )) then
  1785.       begin
  1786.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1787.          ButtonHLColor , ButtonSColor , TextColor );
  1788.         { Initialize new FileIconPanel and call initialize, sending 0 ED }
  1789.         TheFIP := TFileIconPanel.Create( Self );
  1790.         TheFIP.Parent := Self;
  1791.         TheFIP.Initialize((( ColumnCounter - 1 ) * TheIconSpacing ),
  1792.          (( RowCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize ,
  1793.           3 , 7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1794.            TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  1795.         { Increment column counter and if needed row counter }
  1796.         ColumnCounter := ColumnCounter + 1;
  1797.         if ColumnCounter > MaxIconsInARow then
  1798.         begin
  1799.           ColumnCounter := 1;
  1800.           RowCounter := RowCounter + 1;
  1801.         end;
  1802.       end;
  1803.     end;
  1804.   end;
  1805.   { Reset to visible }
  1806.   Visible := true;
  1807. end;
  1808.  
  1809. { Update method for FIPscrollbox }
  1810. procedure TFileIconPanelScrollBox.Update;
  1811. begin
  1812.   IconsNeedRefreshing := true;
  1813.   { Force a repaint }
  1814.   InvalidateRect( TheStoredHandle , nil , true );
  1815. end;
  1816.  
  1817. { Create method for FIPScrollbox }
  1818. constructor TFileIconPanelScrollBox.Create( AOwner : TComponent );
  1819. begin
  1820.   inherited Create( AOwner );
  1821.   TheFWB := TFileWorkBench.Create( Self );
  1822. end;
  1823.  
  1824. { This function returns the next selected file's name }
  1825. function TFileIconPanelScrollBox.GetNextSelection( SourceDirectory : String;
  1826.                            var CurrentItem : Integer ) : String;
  1827. var TheResult    : String;      { Holds result of function }
  1828.     TheComponent : TComponent;  { Used for typecast        }
  1829.     finished     : boolean;     { Loop control variable    }
  1830.     TheComponentCount : Integer;
  1831. begin
  1832.   TheComponentCount := ComponentCount;
  1833.   { If past end of components exit with no result }
  1834.   if CurrentItem > TheComponentCount then TheResult := '' else
  1835.   begin
  1836.     { Set loop counter and run till find match or run out }
  1837.     finished := false;
  1838.     while not finished do
  1839.     begin
  1840.       { Pull component out of the list and check it }
  1841.       TheComponent := Components[ CurrentItem - 1 ];
  1842.       { Increment counter for later }
  1843.       CurrentItem := CurrentItem + 1;
  1844.       { Do the typecast with AS }
  1845.       if TheComponent is TFileIconPanel then
  1846.       with TheComponent as TFileIconPanel do
  1847.       begin
  1848.         { If its selected make sure OK }
  1849.         if Selected then
  1850.         begin
  1851.           { Don't accept backup for this level of operation }
  1852.           if FTheLabel.Caption <> '..' then
  1853.           begin
  1854.             { Otherwise return the name and abort the loop }
  1855.             TheResult := FTheName;
  1856.             finished := true;
  1857.           end;
  1858.         end
  1859.         else
  1860.         begin
  1861.           { Check to see if out of components }
  1862.           if CurrentItem > TheComponentCount then
  1863.           begin
  1864.             { If so signal error and abort }
  1865.             TheResult := '';
  1866.             finished := true;
  1867.           end;
  1868.         end;
  1869.       end;
  1870.     end;
  1871.   end;
  1872.   GetNextSelection := TheResult;
  1873. end;
  1874.  
  1875. { This procedure places a selection of files in the display based on wildcards }
  1876. procedure TFileIconPanelScrollBox.DisplayRecursiveSearchResults(
  1877.            TheStartingDirectory : String );
  1878. var XCounter ,
  1879.     YCounter   : Integer;
  1880.  
  1881. { This procedure does a recursive file search by first getting all matches (in-}
  1882. { cluding directories) and adding them to the list. Then it checks for ALL the }
  1883. { subdirectories and does the same trick on them til there are no more matches }
  1884. { and no more subdirectories, at which point it exits and recurses back up.    }
  1885. procedure RecursiveFileSearch( TheWorkingDirectory : String; var XCounter ,
  1886.                                YCounter : Integer );
  1887.  
  1888. { VITAL!!! These variables MUST be local for recursrion to work! }
  1889. var
  1890.     Finished        : Boolean;         { Loop flag              }
  1891.     TheSR           : TSearchRec;      { Searchrecord for FF/FN }
  1892.     TheResult       : Integer;         { return variable        }
  1893.     TargetPath ,
  1894.     FileMask   ,
  1895.     TheStoredWorkingDirectory ,
  1896.     ModifiedDirectory  : String;       { path for FF/FN         }
  1897.     TheFIP          : TFileIconPanel;  { generic FIP holder     }
  1898.     ButtonColor   ,                    { main panel color       }
  1899.     ButtonHLColor ,                    { bright panel color     }
  1900.     ButtonSColor  ,                    { dark panel color       }
  1901.     Textcolor       : TColor;          { label text color       }
  1902.  
  1903. begin
  1904.   { Set up the initial variables }
  1905.   Finished := false;
  1906.   TheStoredWorkingDirectory := TheWorkingDirectory;
  1907.   Targetpath := ExtractFilePath( TheWorkingDirectory );
  1908.   FileMask := ExtractFileName( TheWorkingDirectory );
  1909.   { Make the call to FindFirst set to get any file }
  1910.   TheResult := FindFirst( TheWorkingDirectory , faAnyFile , TheSR );
  1911.   if TheResult < 0 then finished := true;
  1912.   if (( TheSr.Name <> '.' ) and ( TheSr.Name <> '..' ) and ( TheResult >= 0 ))
  1913.   then begin
  1914.     if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory ) =
  1915.      faDirectory ) then
  1916.     begin { A directory }
  1917.       GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1918.        ButtonHLColor , ButtonSColor , TextColor );
  1919.       { If found create a new FileIconPanel on the imported scrollbox }
  1920.       { Note sending 0 ExtraData parameter to indicate file not drive }
  1921.       TheFIP := TFileIconPanel.Create( Self );
  1922.       TheFIP.Parent := Self;
  1923.       TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1924.        (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize , 3 ,
  1925.         7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor , TargetPath
  1926.          + TheSr.Name , 'MS Serif' , [] , 0 );
  1927.       { Increment column counter and move to new row if past limit }
  1928.       XCounter := XCounter + 1;
  1929.       if XCounter > MaxIconsInARow then
  1930.       begin
  1931.         XCounter := 1;
  1932.         YCounter := YCounter + 1;
  1933.       end;
  1934.     end
  1935.     else
  1936.     begin { A File }
  1937.       { Set up the default color scheme for files }
  1938.       GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1939.        ButtonHLColor , ButtonSColor , TextColor );
  1940.       { If found create a new FileIconPanel on the imported scrollbox }
  1941.       { Note sending 0 ExtraData parameter to indicate file not drive }
  1942.       TheFIP := TFileIconPanel.Create( Self );
  1943.       TheFIP.Parent := Self;
  1944.       TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1945.        (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize, TheIconSize , 3 ,
  1946.         7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor , TargetPath
  1947.          + TheSr.Name , 'MS Serif' , [] , 0 );
  1948.       { Increment column counter and move to new row if past limit }
  1949.       XCounter := XCounter + 1;
  1950.       if XCounter > MaxIconsInARow then
  1951.       begin
  1952.         XCounter := 1;
  1953.         YCounter := YCounter + 1;
  1954.       end;
  1955.     end;
  1956.   end;
  1957.   { loop through all files in the directory and look for matches }
  1958.   while not Finished do
  1959.   begin
  1960.     { Make call to FindNext, using only SearchRecord from FindFirst }
  1961.     TheResult := FindNext( TheSR );
  1962.     { A -1 result means no more files so exit }
  1963.     if TheResult <> 0 then finished := true else
  1964.     begin
  1965.       if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory ) =
  1966.        faDirectory ) then
  1967.       begin { A directory }
  1968.         { Set up the blue color scheme for directories }
  1969.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1970.          ButtonHLColor , ButtonSColor , TextColor );
  1971.         { If found create a new FileIconPanel on the imported scrollbox }
  1972.         { Note sending 0 ExtraData parameter to indicate file not drive }
  1973.         TheFIP := TFileIconPanel.Create( Self );
  1974.         TheFIP.Parent := Self;
  1975.         TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1976.          (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize , 3 ,
  1977.            7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1978.             TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  1979.         { Increment column counter and move to new row if past limit }
  1980.         XCounter := XCounter + 1;
  1981.         if XCounter > MaxIconsInARow then
  1982.         begin
  1983.           XCounter := 1;
  1984.           YCounter := YCounter + 1;
  1985.         end;
  1986.       end
  1987.       else
  1988.       begin { A File }
  1989.         { Set up the default color scheme for files }
  1990.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1991.          ButtonHLColor , ButtonSColor , TextColor );
  1992.         { If found create a new FileIconPanel on the imported scrollbox }
  1993.         { Note sending 0 ExtraData parameter to indicate file not drive }
  1994.         TheFIP := TFileIconPanel.Create( Self );
  1995.         TheFIP.Parent := Self;
  1996.         TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1997.          (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize , 3 ,
  1998.           7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1999.            TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  2000.         { Increment column counter and move to new row if past limit }
  2001.         XCounter := XCounter + 1;
  2002.         if XCounter > MaxIconsInARow then
  2003.         begin
  2004.           XCounter := 1;
  2005.           YCounter := YCounter + 1;
  2006.         end;
  2007.       end;
  2008.     end;
  2009.   end;
  2010.   { Set up the variables to do recursive calls on all directories}
  2011.   Finished := false;
  2012.   ModifiedDirectory := ExtractFilePath( TheWorkingdirectory ) + '*.*';
  2013.   { Make the call to FindFirst set to get any file, ignore result }
  2014.   TheResult := FindFirst( ModifiedDirectory , faDirectory , TheSR );
  2015.   while not Finished do
  2016.   begin
  2017.     { Make call to FindNext, using only SearchRecord from FindFirst }
  2018.     TheResult := FindNext( TheSR );
  2019.     { A -1 result means no more files so exit }
  2020.     if TheResult <> 0 then finished := true
  2021.     else
  2022.     begin
  2023.       if TheSR.Name <> '..' then { Ignore backup in this case }
  2024.       begin
  2025.         { Do second check due to bug in FindNext }
  2026.         if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory )
  2027.         = faDirectory ) then
  2028.         begin
  2029.           { Set up modified directory to recurse into }
  2030.           ModifiedDirectory := ExtractFilePath( TheStoredWorkingDirectory ) +
  2031.            TheSR.Name + '\' + FileMask;
  2032.           { Perform the recursion }
  2033.           RecursiveFileSearch( ModifiedDirectory , XCounter , YCounter );
  2034.         end;
  2035.       end;
  2036.     end;
  2037.   end;
  2038. end;
  2039.  
  2040. begin
  2041.   { Keep the scrollbox from updating during refresh }
  2042.   Visible := false;
  2043.   { Make the clear call }
  2044.   ClearTheFIPs;
  2045.   XCounter := 1;
  2046.   YCounter := 1;
  2047.   { Get the drives for the current machine }
  2048.   AddDriveIcons( XCounter , YCounter );
  2049.   RecursiveFileSearch( TheStartingDirectory , XCounter , YCounter );
  2050.   { Make the scrollbox visible again }
  2051.   Visible := true;
  2052. end;
  2053.  
  2054. end.
  2055.